import api from './api.js'

const form = $('#newArticle')
form.submit((e) => {
  e.preventDefault()
  $('.alert.alert-success').hide()
  $('.alert.alert-warning').hide()
  $('.alert.alert-danger').hide()

  const arr = form.serializeArray()
  let data = {}
  arr.forEach(item => {
    data[item.name] = item.value
  })
  data.createtime = new Date()

  console.log(data)

  let result = api.newArticle(data)
  result.then(res => {
    if (res.errno === 0) {
      console.log(res)
      $('.alert.alert-success').show()
      setTimeout(() => {
        location.href = '/detail.html?id=' + res.data.id
      }, 1500);
    } else {
      $('.alert.alert-danger').show()
      console.log(res)
    }
  }).catch(err => {
    console.error(err)
  })
})

